package YOONA;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;

@WebServlet("/aaa")
public class a implements Servlet {

    public void init(ServletConfig servletConfig) throws ServletException {

    }

    public ServletConfig getServletConfig() {
        return null;
    }
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        String url = "jdbc:mysql://localhost:3306/yoona";
        String user = "root";
        String password = "123456";

        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 建立连接
            Connection connection = DriverManager.getConnection(url, user, password);

            // 创建Statement对象
            Statement statement = connection.createStatement();

            // 执行查询语句，获取结果集
            ResultSet resultSet = statement.executeQuery("SELECT name, age FROM student");

            // 遍历结果集，输出name和age
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                System.out.println("Name: " + name + ", Age: " + age);
            }

            // 关闭资源
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public String getServletInfo() {
        return null;
    }

    public void destroy() {

    }
}
